# coding:utf-8
import os
import sys


from PyQt5.QtCore import Qt, QTranslator
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QFontDatabase
from qfluentwidgets import FluentTranslator

from app.common.config import cfg

from app.view.register_window import RegisterWindow
from app.ui_controller.ui_pool import Ui_Pool, Portal_Pool

# enable dpi scale
if cfg.get(cfg.dpiScale) != "Auto":
    os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
    os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))
else:
    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
    )
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

# create application
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_DontCreateNativeWidgetSiblings)

# internationalization
locale = cfg.get(cfg.language).value
translator = FluentTranslator(locale)
galleryTranslator = QTranslator()
galleryTranslator.load(locale, "app", ".", ":/app/i18n")

# custom font
fontDb = QFontDatabase()
fontNames = ["harmonyos"]
for fontname in fontNames:
    fontID = fontDb.addApplicationFont(f":app/fonts/{fontname}.ttf")

fontFamilies = fontDb.applicationFontFamilies(fontID)
print(fontFamilies)

app.installTranslator(translator)
app.installTranslator(galleryTranslator)
# w = MainWindow()

# create main window
w = RegisterWindow()

main_pool = Ui_Pool("main_pool")
Portal_Pool.portals["main_pool"] = main_pool

main_pool.set_objectByName("app", app)
main_pool.set_objectByName("reswin", w)

print(main_pool.pool.keys())


w.show()

app.exec()
